#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.pith.packets.meta import Meta
from oriolus.pith.packets.packet import Packet, Status
from oriolus.docker.docker.remove import RemoveCommand, RemoveError
from oriolus.docker.scripts.runner import RunnerError
from oriolus.pith.packets.script import Script
from pmock import *
from unittest import TestCase, TestLoader, TextTestRunner

class TestRemove(TestCase):
    def test_remove1(self):
        mock_script_runner = Mock()
        remove_command = RemoveCommand(mock_script_runner)
        scritps = [Script("preremove", "preremove_path"),
                   Script("postremove", "postremove_path"), 
                   Script("remove", "remove_path")]
        packet = Packet(Meta("packet", "1.2.3.4", None, [], [], []), scritps)

        mock_script_runner.expects(once()).prepare().id(0)
        mock_script_runner.expects(once()).run(eq(scritps[0]), eq(None)).after(0).id(1)
        mock_script_runner.expects(once()).run(eq(scritps[2]), eq(None)).after(1).id(2)
        mock_script_runner.expects(once()).run(eq(scritps[1]), eq(None)).after(2).id(3)

        remove_command.execute(packet, None)
        mock_script_runner.verify()

        self.assertEqual(Status.REMOVED, packet.status)

    def test_remove2(self):
        mock_script_runner = Mock()
        remove_command = RemoveCommand(mock_script_runner)
        scritps = [Script("postremove", "postremove_path"),
                   Script("preinstall", "preinstall_path"), 
                   Script("remove", "remove_path"), 
                   Script("preremove", "preremove_path"), 
                   Script("postinstall", "postinstall_path"),
                   Script("install", "install_path")]
        packet = Packet(Meta("packet", "1.2.3.4", None, [], [], []), scritps)

        mock_script_runner.expects(once()).prepare().id(0)
        mock_script_runner.expects(once()).run(eq(scritps[3]), eq(None)).after(0).id(1)
        mock_script_runner.expects(once()).run(eq(scritps[2]), eq(None)).after(1).id(2)
        mock_script_runner.expects(once()).run(eq(scritps[0]), eq(None)).after(2).id(3)

        remove_command.execute(packet, None)
        mock_script_runner.verify()

        self.assertEqual(Status.REMOVED, packet.status)

    def test_remove3(self):
        mock_script_runner = Mock()
        remove_command = RemoveCommand(mock_script_runner)
        scritps = [Script("preinstall", "preinstall_path"), 
                   Script("remove", "remove_path"),
                   Script("postinstall", "postinstall_path")]
        packet = Packet(Meta("packet", "1.2.3.4", None, [], [], []), scritps)

        mock_script_runner.expects(once()).prepare().id(0)
        mock_script_runner.expects(once()).run(eq(scritps[1]), eq(None)).after(0).id(1)

        remove_command.execute(packet, None)
        mock_script_runner.verify()

        self.assertEqual(Status.REMOVED, packet.status)

    def test_remove4(self):
        mock_script_runner = Mock()
        remove_command  = RemoveCommand(mock_script_runner)
        scritps = []
        packet = Packet(Meta("packet", "1.2.3.4", None, [], [], []), scritps)

        mock_script_runner.expects(once()).prepare()

        remove_command .execute(packet, None)
        mock_script_runner.verify()

        self.assertEqual(Status.REMOVED, packet.status)

    def test_remove5(self):
        mock_script_runner = Mock()
        remove_command  = RemoveCommand(mock_script_runner)
        scritps = [Script("remove", "remove_path"),
                   Script("preinstall", "preinstall_path"), 
                   Script("postremove", "postremove_path"),
                   Script("postinstall", "postinstall_path")]
        packet = Packet(Meta("packet", "1.2.3.4", None, [], [], []), scritps)

        mock_script_runner.expects(once()).prepare().id(0)
        mock_script_runner.expects(once()).run(eq(scritps[0]), eq(None)).will(raise_exception(RunnerError("Test script runner error..."))).after(0)

        self.assertRaises(RemoveError, remove_command.execute, packet, None)
        mock_script_runner.verify()

        self.assertEqual(Status.BAD, packet.status)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestRemove)
    TextTestRunner(verbosity=2).run(suite)